﻿mapl = {
	url:window.location.href,
	server:window.location.href.match(/^.*?:\/\/.*?\//)[0],
	staticServer:{
		'file:///':'',
		'http://localhost/':'',
		// this assigns the static server base url that images and swf should be loaded
		// most small setups will have both of these values the same
		'http://www.livesite.com/':'http://static.mediaserver.com/static-files/'
	},
	init:function(){
		// assigns the appropriate static server if one
		mapl.staticServer = mapl.staticServer[mapl.server];
	
		this.addDomInit(this.baseDomInit.bind(this));
		this.domInit(document);
		
		this.addDomInit(this.appInit);
		this.appInit(document);
	},
	apps:{
		/* example of a typical mapl include line */
		// UniqueAppName:{name:'Name of the App',triggers:['.css2-selector'],dependencies:['ArrayOf','UniqueMapNames'],url:'js/apps/UniqueAppName.js'},
		// MappingLayer:{name:'Dom Configed Google Mapping Layer',triggers:['.imap'],dependencies:null,url:'js/apps/MappingLayer.js'},
		// MapCoordPicker:{name:'Django Admin Map Coord Picker',triggers:['.map-multi-select','.map-single-select'],dependencies:['MappingLayer'],url:'js/apps/MapCoordPicker.js'},
	},
	appInit:function(root){
		$H(mapl.apps).each(function(app,appName){
			app.triggers.each(function(scan){
				if(root.getElement(scan) && !app.active){
					if(app.dependencies){
						app.dependencies.each(function(dependant){
							if(!mapl.apps[dependant].active){
								mapl.appLoad(mapl.apps[dependant]);
							}
						});
					}
					mapl.appLoad(app);
				}
			});
		});
	},
	appLoad:function(app){
		app.active = true;
		if(app.url.match('://')){
			new Asset.javascript(app.url);
		}else{
			new Asset.javascript(mapl.staticServer+app.url);
		}
	},
	baseDomInit:function(root){
		/*
		** example of typical DomInit		
		root.getElements('.css2-selector').each(function(s){
			new mapl.classes.UniqueAppName(s,options);
		});
		root.getElements('.map-multi-select').each(function(s){
			new mapl.classes.MapCoordPicker(s,true);
		});
		*/
	},
	addDomInit:function(fn){
		this.domInits.push(fn);
		return this.domInits.getLast();
	},
	domInit:function(root){
		mapl.domInits.each(function(s){
			s(root);
		});
	},
	domInits:[],
	classes:{}
}

window.addEvent('domready',mapl.init.bind(mapl));